/*
	Author: Superxpdude
	
	Description:
	Checks if the client is authorised by the server to perform an action.
	Requires server-side publicVariableEventHandlers to be set up.
	
	Parameters:
	_this select 0: UID of Player requesting authorisation
	_this select 1: Unit of Player requesting authorisation
	_this select 2 (Optional): Type of request in string (default: basic yes/no response)
	
	Returns:
	BOOL representing if client is authorised or not, or if request has been fulfilled.
*/
private ["_playerUID","_player","_request","_accepted","_error"];

// Clear variables to use later
adminRequest = nil;
adminResponse = nil;

// Define variables to send to server
_playerUID = [_this, 0, nil, [0]] call BIS_fnc_param;
_player = [_this, 1, objnull, [objnull]] call BIS_fnc_param;
_request = [_this, 2, "auth", [""]] call BIS_fnc_param;

// Check for invalid variables
if (isNil _playerUID) exitwith {["PlayerUID must be defined when calling the auth function."] call BIS_fnc_error; false};
if (isNull _player) exitwith {["Player unit must be defined when calling the auth function."] call BIS_fnc_error; false};

// Compile and send the auth request to the server (it only needs to go to the server)
adminRequest = [_playerUID, _player, _request];
publicVariableServer "adminRequest";

// Wait for our response from the server
waitUntil {!isNil "adminResponse"};

// Process the response
_accepted = adminResponse select 2;
_error = adminResponse select 3;
if (_error == true) exitWith {["The server had an error processing the auth request."] call BIS_fnc_error; false};
if ((typeName _accepted) != "BOOL") exitWith {["Server must return a bool as a response to the auth request."] call BIS_fnc_error; false};
if (_playerUID != (adminResponse select 0)) exitWith {["Server must the same playerUID as was originally sent."] call BIS_fnc_error; false};
if (_player != (adminResponse select 1)) exitWith {["Server must return the same player object as was originally sent."] call BIS_fnc_error; false};

// If the response passes all of the validity checks, delete the variables we no longer need
adminRequest = nil;
adminResponse = nil;

// If everything checks out, return the auth status to the calling script
_accepted